package by.grsu.hdm.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hdm.exception.MyExceptions;
import by.grsu.hdm.media.AudioFile;
import by.grsu.hdm.media.MediaData;
import by.grsu.hdm.media.MediaPlayer;
import by.grsu.hdm.media.VideoFile;


public class OtherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OtherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MediaPlayer mp = new MediaPlayer();
		MediaData data = new MediaData("Poison", "Prodigy", "Some data");
		mp.addFile(new AudioFile(data));
		data = new MediaData("World is mine", "David Guetta", "Some data");
		mp.addFile(new AudioFile(data));
		data = new MediaData("Comin' in hot", "Hollywood Undead", "Some data");
		mp.addFile(new AudioFile(data));
		data = new MediaData("How it's work", "Discovery", "Some data");
		mp.addFile(new VideoFile(data));
		
		response.setContentType("text/html");
        
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("<title>MediaPlayer Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>PlayList</h3>");
        if (request.getParameter("id")!=null)
        {
        	try {
        		int id = Integer.parseInt( request.getParameter("id"));
        			out.println(mp.play(id)+"<br>");
				
			} catch (MyExceptions e) {
				out.println(e.getMessage()+"<br>");
			}
        }
        else
        {
        	try {
        		for (int i = 0; i < mp.getCountFiles(); i++) {
        			out.println(mp.play(i)+"<br>");
				}
			} catch (MyExceptions e) {
				out.println(e.getMessage()+"<br>");
			}
        }
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
